package com.ogawa.projectcommon.datacollect

import com.ogawa.base.utils.SpUtil
import com.ogawa.projectcommon.datacollect.bean.*
import com.ogawa.projectcommon.network.RetrofitManager
import com.ogawa.projectcommon.utils.ProjectSpUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

object DataCollectUtils {
    /**
     * 程序启动事件收集
     *
     * @param program     程序指令
     * @param programName 程序名称
     * @param type        程序类型
     * @param start       开始/结束
     */
    fun programChange(
        program: String?,
        programName: String?,
        type: Int,
        start: Boolean,
        deviceType: String
    ) {
        val changeBean = ProgramEventBean()
        changeBean.program = program
        changeBean.programName = programName
        changeBean.type = type
        changeBean.status = if (start) 1 else 2
        changeBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.eventValue = changeBean
        bean.type = EventConstant.START_PROGRAM
        eventUpdate(bean)
    }

    /**
     * 坐姿调整事件收集
     *
     * @param pos      坐姿模式
     */
    fun seatEvent(pos: Int, deviceType: String) {
        val eventBean = SeatEventBean()
        eventBean.model = pos
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.type = EventConstant.SEAT_ADJUST
        bean.eventValue = eventBean
        eventUpdate(bean)
    }

    /**
     * 灯光事件收集
     *
     * @param command  灯光命令
     */
    fun lightEvent(command: String?, deviceType: String) {
        val eventBean = LightEventBean()
        eventBean.command = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.type = EventConstant.LIGHT
        bean.eventValue = eventBean
        eventUpdate(bean)
    }

    /**
     * 定位事件收集
     *
     * @param model    定位模式
     * @param y        定点位置
     * @param command  程序命令
     */
    fun position(model: Int, y: Int, command: String?, deviceType: String) {
        val eventBean = PositionEventBean()
        eventBean.model = model
        eventBean.fixedPosition = y
        eventBean.program = command
        eventBean.type = 4
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.type = EventConstant.POSITION
        bean.eventValue = eventBean
        eventUpdate(bean)
    }

    /**
     * 按摩宽度事件收集
     *
     * @param width   机芯宽度
     * @param command 程序命令
     */
    fun widthEvent(width: Int, command: String?, deviceType: String) {
        val eventBean = WidthEventBean()
        eventBean.widthGear = width
        eventBean.type = 4
        eventBean.program = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.eventValue = eventBean
        bean.type = EventConstant.WIDTH
        eventUpdate(bean)
    }

    /**
     * 4D强度事件收集
     *
     * @param strength    4d强度
     * @param type        程序类型
     * @param programName 程序名称
     * @param command     程序指令
     */
    fun strengthEvent(
        strength: Int,
        type: Int,
        programName: String?,
        command: String?,
        deviceType: String
    ) {
        val eventBean = StrengthEventBean()
        eventBean.strength = strength
        eventBean.type = type
        eventBean.programName = programName
        eventBean.program = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.eventValue = eventBean
        bean.type = EventConstant._4D_STRENGTH
        eventUpdate(bean)
    }

    /**
     * 气压强度事件收集
     *
     * @param strength    4d强度
     * @param type        程序类型
     * @param programName 程序名称
     * @param command     程序指令
     */
    fun gasStrengthEvent(
        strength: Int,
        type: Int,
        programName: String?,
        command: String?,
        deviceType: String
    ) {
        val eventBean = GasStrengthEventBean()
        eventBean.strength = strength
        eventBean.type = type
        eventBean.programName = programName
        eventBean.program = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.eventValue = eventBean
        bean.type = EventConstant.GAS_STRENGTH
        eventUpdate(bean)
    }

    /**
     * 敲击速度事件收集
     *
     * @param speed       敲击速度
     * @param type        程序类型
     * @param programName 程序名称
     * @param command     程序指令
     */
    fun knockSpeedEvent(
        speed: Int,
        type: Int,
        programName: String?,
        command: String?,
        deviceType: String
    ) {
        val eventBean = KnockSpeedEventBean()
        eventBean.speed = speed
        eventBean.type = type
        eventBean.programName = programName
        eventBean.program = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.eventValue = eventBean
        bean.type = EventConstant.KNOCK_SPEED
        eventUpdate(bean)
    }

    /**
     * 揉捏速度事件收集
     *
     * @param speed       揉捏速度
     * @param type        程序类型
     * @param programName 程序名称
     * @param command     程序指令
     */
    fun kneadSpeedEvent(
        speed: Int,
        type: Int,
        programName: String?,
        command: String?,
        deviceType: String
    ) {
        val eventBean = KneadSpeedEventBean()
        eventBean.speed = speed
        eventBean.type = type
        eventBean.programName = programName
        eventBean.program = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.eventValue = eventBean
        bean.type = EventConstant.KNEAD_SPEED
        eventUpdate(bean)
    }

    /**
     * 高级按摩事件收集
     *
     * @param operationName 操作名称
     * @param command       指令
     */
    fun seniorMassageEvent(
        operationName: String?,
        command: String?,
        deviceType: String
    ) {
        val eventBean = SeniorMassageEventBean()
        eventBean.operationName = operationName
        eventBean.command = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.eventValue = eventBean
        bean.type = EventConstant.SENIOR_MASSAGE
        eventUpdate(bean)
    }

    /**
     * 温感部位事件收集
     *
     * @param position    部位
     * @param status      开关状态
     * @param type        程序类型
     * @param programName 程序名称
     * @param command     指令
     */
    fun warmEvent(
        position: String?,
        status: Int,
        type: Int,
        programName: String?,
        command: String?,
        deviceType: String
    ) {
        val eventBean = WarmEventBean()
        eventBean.position = position
        eventBean.switchStatus = status
        eventBean.type = type
        eventBean.programName = programName
        eventBean.program = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.type = EventConstant.WARM
        bean.eventValue = eventBean
        eventUpdate(bean)
    }

    /**
     * 脚底滚轮事件收集
     *
     * @param status      开关状态
     * @param type        程序类型
     * @param programName 程序名称
     * @param command     指令
     */
    fun footRollEvent(
        status: Int,
        type: Int,
        programName: String?,
        command: String?,
        deviceType: String
    ) {
        val eventBean = FootRollEventBean()
        eventBean.switchStatus = status
        eventBean.type = type
        eventBean.programName = programName
        eventBean.program = command
        eventBean.deviceType = deviceType
        val bean = BaseEventBean()
        bean.type = EventConstant.FOOT_ROLL
        bean.eventValue = eventBean
        eventUpdate(bean)
    }

    /**
     * 登录事件收集
     *
     * @param userType
     * @param name
     * @param mobile
     */
    fun loginEvent(
        userType: String?,
        name: String?,
        mobile: String?
    ) {
        val eventBean = LoginEventBean()
        eventBean.userType = userType
        eventBean.mobile = mobile
        eventBean.name = name
        val bean = BaseEventBean()
        bean.type = EventConstant.LOGIN
        bean.eventValue = eventBean
        eventUpdate(bean)
    }

    /**
     * 退出登录事件收集
     *
     */
    fun logoutEvent() {
        val bean = BaseEventBean()
        bean.type = EventConstant.LOGOUT
        eventUpdate(bean)
    }

    /**
     * 收藏事件收集
     *
     * @param id   收藏ID
     * @param name 名称
     * @param type 收藏类型
     */
    fun collectEvent(id: String?, name: String?, type: String?) {
        val eventBean = CollectEventBean()
        eventBean.id = id
        eventBean.name = name
        eventBean.type = type
        val bean = BaseEventBean()
        bean.type = EventConstant.COLLECT
        bean.eventValue = eventBean
        eventUpdate(bean)
    }

    private fun eventUpdate(bean: BaseEventBean) {
        val sn = SpUtil.get(SpUtil.SN, "") as String
        val userInfo = ProjectSpUtils.getUserInfo()
        bean.sn = sn
        if (!userInfo.isVisitor){
            bean.userId = userInfo.user.id
        }else{
            bean.userId= (-1).toString()
        }
        GlobalScope.launch {
            val dataCollectServer =
                RetrofitManager.INSTANCE.createService(DataCollectApiService::class.java)
            dataCollectServer.eventUpdate(bean)
        }
    }
}